package com.cskaoyan.order.biz.handler;

import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.order.biz.context.CancelOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

/**
 * @author zhuqiushi
 * @since 2022/09/21 16:37
 */
@Component
@Slf4j
public class CancelUpdateOrderItemHandler extends AbstractTransHandler{
    @Autowired
    OrderItemMapper orderItemMapper;
    @Override
    public boolean isAsync() {
        return false;
    }

    @Override
    public boolean handle(TransHandlerContext context) {
        CancelOrderContext cancelOrderContext = (CancelOrderContext) context;
        //在此修改order-item表
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("orderId", cancelOrderContext.getOrderId());
        OrderItem orderItem = new OrderItem();
        orderItem.setStatus(2);
        int affectRows = orderItemMapper.updateByExampleSelective(orderItem, example);
        if (affectRows < 1) {
            throw new BizException(OrderRetCode.DB_EXCEPTION.getCode(), OrderRetCode.DB_EXCEPTION.getMessage());
        }
        return true;
    }
}
